package com.hanson.demo.actors.test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import com.hanson.demo.actors.ActorRef;
import com.hanson.demo.actors.ActorSystemServiceContext;
import com.hanson.demo.actors.Message;
import com.hanson.demo.actors.biz_actor.MyOrderActor;
import com.hanson.demo.actors.biz_actor.MyPersonActor;
import com.hanson.demo.actors.system.ActorSystem;


public class MainTs {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		ActorSystemServiceContext actorSystemServiceContext = new ActorSystemServiceContext();
		ExecutorService executor = Executors.newFixedThreadPool(4); // 并发任务
		ActorSystem system = new ActorSystem();

		ActorRef actorOrder = system.createActor("actorOrder", () -> new MyOrderActor(actorSystemServiceContext));
		ActorRef actorPerson = system.createActor("actorPerson", () -> new MyPersonActor(actorSystemServiceContext));

		for (int t = 0; t < 10; t++) {
			final int taskId = t;
			Future<?> future = executor.submit(() -> {
				for (int i = 0; i < 101; i++) {
					try {
						Thread.sleep(10);//调节时间  可以出现 MessageBox is busy
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if (i % 20 == 0) {// 先消费person 然后进订单
						actorOrder.tell(new Message("send msg " + taskId + " hanson" + i));
						actorPerson.tell(new Message("send msg " + taskId + " hanson" + i));
					} else if (i % 2 == 0) { // 独立消费订单
						actorOrder.tell(new Message("send msg " + taskId + " hanson" + i));
					} else { // 独立消费Person
						actorPerson.tell(new Message("send msg " + taskId + " hanson" + i));
					}
				}
			});
			// 可选：等待所有任务完成
			future.get();
		}
		Thread.sleep(2000);
		system.stop(actorOrder);
		system.stop(actorPerson);

	}
}
